package com.central.scheduler.master.processor;

import com.central.scheduler.common.enums.ExecutionStatus;
import com.central.scheduler.master.cache.TaskInstanceCacheManager;
import com.central.scheduler.master.cache.impl.TaskInstanceCacheManagerImpl;
import com.central.scheduler.master.processor.queue.TaskResponseEvent;
import com.central.scheduler.master.processor.queue.TaskResponseService;
import com.central.scheduler.master.runner.WorkflowExecuteThread;
import com.central.scheduler.remote.command.Command;
import com.central.scheduler.remote.command.CommandType;
import com.central.scheduler.remote.command.TaskExecuteResponseCommand;
import com.central.scheduler.remote.processor.NettyRequestProcessor;
import com.central.scheduler.service.bean.SpringApplicationContext;
import com.central.scheduler.common.utils.JSONUtils;
import com.google.common.base.Preconditions;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Tindy
 * @date 2021/11/1
 * @describe
 * task response processor
 */
public class TaskResponseProcessor implements NettyRequestProcessor {

    private final Logger logger = LoggerFactory.getLogger(TaskResponseProcessor.class);
    /**
     * process service
     */
    private final TaskResponseService taskResponseService;

    /**
     * taskInstance cache manager
     */
    private final TaskInstanceCacheManager taskInstanceCacheManager;

    public TaskResponseProcessor() {
        this.taskResponseService = SpringApplicationContext.getBean(TaskResponseService.class);
        this.taskInstanceCacheManager = SpringApplicationContext.getBean(TaskInstanceCacheManagerImpl.class);
    }

    public void init(ConcurrentHashMap<Long, WorkflowExecuteThread> processInstanceExecMaps) {
        this.taskResponseService.init(processInstanceExecMaps);
    }
    /**
     * task final result response
     * need master process , state persistence
     *
     * @param channel channel
     * @param command command
     */
    @Override
    public void process(Channel channel, Command command) {
        Preconditions.checkArgument(CommandType.TASK_EXECUTE_RESPONSE == command.getType(), String.format("invalid command type : %s", command.getType()));

        TaskExecuteResponseCommand responseCommand = JSONUtils.parseObject(command.getBody(), TaskExecuteResponseCommand.class);
        logger.info("received command : {}", responseCommand);

        taskInstanceCacheManager.cacheTaskInstance(responseCommand);

        // TaskResponseEvent
        TaskResponseEvent taskResponseEvent = TaskResponseEvent.newResult(ExecutionStatus.of(responseCommand.getStatus()),
                responseCommand.getEndTime(),
                responseCommand.getProcessId(),
                responseCommand.getAppIds(),
                responseCommand.getTaskInstanceId(),
                responseCommand.getVarPool(),
                channel,
                responseCommand.getProcessInstanceId()
        );
        taskResponseService.addResponse(taskResponseEvent);
    }
}
